package Server5;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    private Socket socket;


    private Client(){
        try {
            System.out.println("正在链接");
            socket=new Socket("176.130.21.29",8088);//老板ip：176.130.21.37  毛孔ip：176.130.21.28   老板笔记本176.130.21.33
            System.out.println("链接成功" );
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void start(){
        try{

            ServerHandler sh=new ServerHandler();
            Thread t=new Thread(sh);//这里用于接收
            t.start();//启动接收线程

            OutputStream ops=socket.getOutputStream();
            OutputStreamWriter osw=new OutputStreamWriter(ops,"UTF-8");
            BufferedWriter bw=new BufferedWriter(osw);
            PrintWriter pw=new PrintWriter(bw,true);
            Scanner sc=new Scanner(System.in);
            while (true){
                /*System.out.println("输入0退出");
                String line=sc.nextLine();
                if ("0".equals(line)){
                    t.interrupt();
                    break;
                }*/

                pw.println("只因你太美，oh，baby");
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Client client=new Client();
        client.start();
    }

    private class ServerHandler implements Runnable{

        @Override
        public void run() {
            try {
                InputStream is=socket.getInputStream();
                InputStreamReader isr=new InputStreamReader(is,"UTF-8");
                BufferedReader br=new BufferedReader(isr);
                String line;
                while ((line= br.readLine())!=null){
                    System.out.println(line);
                }
            } catch (IOException e) {
                //e.printStackTrace();
            }
        }
    }


}
